import axios from 'axios';
import qs from 'qs';
//调用loading
import { Loading, MessageBox } from 'element-ui';
import utils from 'common/js/util.js'
import { ApiTaskQueue } from "./apitask.js";
// 请求队列
let apiTaskQueue = new ApiTaskQueue(Loading);


const config = {
  headers: {
    'Content-Type': 'application/json;charset=UTF-8',
    //   'Content-Type': 'text/xmlcharset=UTF-8',
  },
  responseType: 'json',
  // timeout: 20000,
  withCredentials: true,
}
// http request 拦截器
axios.interceptors.request.use(
  a => {
    console.log(a);
    // 保存取消请求的接口
    if (a.url_type && a.url_type.name && a.apiCancelToken){
      apiTaskQueue.addCancelTokenTask(a.url_type, a.apiCancelToken);
    }
    // if (a.fullScreenLoading) {
    //   // console.log(a.url_type);
      apiTaskQueue.showFullScreenLoading(a.url)
    // }
    return a;
  },
  err => {
    console.log(err);
    return Promise.reject(err);
  }
);
// 退出登录的提示
let open_msgBox_id = 0;
const reset_Login = function (response){
  console.log(response);
  if (response.data && response.data.rqtype === 'checklogin') {
    return false;
  }
  if (open_msgBox_id) return false;
  open_msgBox_id = 1;
  MessageBox({
    title: '退出登录提示',
    message: '系统检查出自动退出登录',
    showConfirmButton: true,
    showClose:false,
    closeOnClickModal:false,
    closeOnPressEscape:false,
    showCancelButton:false,
    callback: function (action) {
      if (action === 'confirm'){
        open_msgBox_id = 0;
        if (response.config && response.config.data.includes('type=checklogin')) return response;
        util.logout();
      }
    }
  })
};
let closeMessage = false;
// http response 拦截器
axios.interceptors.response.use(
    response => {
        console.log(response)
      const r_config = response.config;
      if (r_config && r_config.url) {
        apiTaskQueue.tryHideFullScreenLoading(r_config.url);
      }
      if (response.status === 200){
        if (response.data && response.data.code === 601){
          // 拦截是否是查询登录状态接口！
          reset_Login(response);
          return response;
        }
        return response;
      } else{
        console.log(response);
      }
    },
    err => {
        console.log(err);
        closeMessage = false;
        if(err.response) {
            utils.tipsMsg('error',err.response.statusText)
        }
      
      let ren_response = {err:err};
      if (err.response && err.response.config && err.response.config.fullScreenLoading){
        apiTaskQueue.tryHideFullScreenLoading(err.response.config.url_type);
        ren_response = err.response || {}
      } else if (err.message && err.message.includes('timeout')){
        apiTaskQueue.tryHideFullScreenLoading('close');
        closeMessage = true;
        ren_response = Object.assign(ren_response, { code: '10001', msg: '网络请求超时！'});   // 返回接口返回的错误信息
      } else if (err.message === 'Network Error'){
        apiTaskQueue.tryHideFullScreenLoading('close');
        closeMessage = true;
        ren_response = Object.assign(ren_response, { code: '10002', msg: '网络连接异常，请检查网络连接！' });
      } else {
        return Promise.reject(err)
      }
      // myAlert(err.message);
      return Promise.resolve(ren_response)   // 返回接口返回的错误信息
  }
)
const errFn = (res) => {
  console.log('状态码：' + res.code + ' 提示：' + res.msg);
  myAlert(res, closeMessage);
  return res;
};
const baseurl = process.env.NODE_ENV === "development" ? 'proxyApi' : '/RcService.asmx' ;

// 定义登陆的接口
const login = (params) => {
    return axios.post(baseurl+ '/SignIn',params,config).then((res) => {
        return res && res.data
    })
};
// 定义登陆的接口
const teacherLogin = (params) => {
    return axios.post(baseurl+ '/Teacher_SignIn',params,config).then((res) => {
        return res && res.data
    })
};
// 退出登陆的接口
const logout = (params) => {
    return axios.post(baseurl+ '/SignOut',params).then((res) => {
        return res && res.data
    })
};
const addAuthority = (params) => {
    // 获取本地的登陆权限
    let info = utils.getUserInfo();
    let bUserName = info.username || '';
    let bDeptID = info.deptid || '';
    let bAddUser = info.adduser || '';
    return {...params,bUserName,bDeptID,bAddUser}
}
// 教室
const addClassRoom = (params) => {
    return axios.post(baseurl+ '/add_rc_classroom',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getClassRoom = (params) => {
    return axios.post(baseurl+ '/get_rc_classroom_list',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const deletClassRoom = (params) => {
    return axios.post(baseurl+ '/del_rc_classroom',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const editClassRoom = (params) => {
    return axios.post(baseurl+ '/edit_rc_classroom',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
// 教师
const addteacher = (params) => {
    return axios.post(baseurl+ '/add_rc_teacher',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getteacher = (params) => {
    return axios.post(baseurl+ '/get_rc_teacher_list',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const deletteacher = (params) => {
    return axios.post(baseurl+ '/del_rc_teacher',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const editteacher = (params) => {
    return axios.post(baseurl+ '/edit_rc_teacher',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
// 学生档案
const getstudylist = (params) => {
    return axios.post(baseurl+ '/get_rc_student_list',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const addstudylist = (params) => {
    return axios.post(baseurl+ '/add_rc_student',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const deletstudylist = (params) => {
    return axios.post(baseurl+ '/del_rc_student',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const editstudylist = (params) => {
    return axios.post(baseurl+ '/edit_rc_student',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getstudyInfo = (params) => {
    return axios.post(baseurl+ '/get_rc_student_model',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getstudyRehabilitationInfo = (params) => {
    return axios.post(baseurl+ '/get_rc_student_Rehabilitation_list',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const addStudyRehabilitation = (params) => {
    return axios.post(baseurl+ '/add_rc_student_Rehabilitation',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const editStudyRehabilitation = (params) => {
    return axios.post(baseurl+ '/edit_rc_student_Rehabilitation',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const deletStudyRehabilitation = (params) => {
    return axios.post(baseurl+ '/del_rc_student_Rehabilitation',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
// 教学计划
const getPlanList = (params) => {
    return axios.post(baseurl+ '/get_rc_teaching_plan_list',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const addPlanList = (params) => {
    return axios.post(baseurl+ '/add_rc_teaching_plan',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const editPlanList = (params) => {
    return axios.post(baseurl+ '/edit_rc_teaching_plan',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const deletPlanList = (params) => {
    return axios.post(baseurl+ '/del_rc_teaching_plan',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const lookPlanList = (params) => {
    return axios.post(baseurl+ '/get_rc_teaching_plan',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
// 获取学生列表

const getStudyListsAll = (params) => {
    return axios.post(baseurl+ '/get_rc_student_list1',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
// 获取教师列表

const getTeacherListsAll = (params) => {
    return axios.post(baseurl+ '/get_rc_teacher_list1',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
// 工作总结
const getworklist = (params) => {
    return axios.post(baseurl+ '/get_rc_work_list',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
} 
const addworklist = (params) => {
    return axios.post(baseurl+ '/add_rc_work',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const deletworklist = (params) => {
    return axios.post(baseurl+ '/del_rc_work',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getworkinfo = (params) => {
    return axios.post(baseurl+ '/get_rc_work_model',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const editworkinfo = (params) => {
    return axios.post(baseurl+ '/edit_rc_work',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getSysInfo = (params) => {
    return axios.post(baseurl+ '/get_sys_code',params,config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
}
// center
const getCenterList = (params) => {
    return axios.post(baseurl+ '/get_rc_teaching_Courseware_list',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const deletCenterList = (params) => {
    return axios.post(baseurl+ '/del_rc_teaching_Courseware',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const addCenterList = (params) => {
    return axios.post(baseurl+ '/add_rc_teaching_Courseware',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const editCenterList = (params) => {
    return axios.post(baseurl+ '/edit_rc_teaching_Courseware',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getCenterInfo = (params) => {
    return axios.post(baseurl+ '/get_rc_teaching_Courseware_model',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};

// 活动
const getactivityList = (params) => {
    return axios.post(baseurl+ '/get_rc_activity_list',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const deletactivityList = (params) => {
    return axios.post(baseurl+ '/del_rc_activity',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const addactivityList = (params) => {
    return axios.post(baseurl+ '/add_rc_activity',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const editactivityinfo = (params) => {
    return axios.post(baseurl+ '/edit_rc_activity',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getactivityInfo = (params) => {
    return axios.post(baseurl+ '/get_rc_activity_model',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
// home
const gettj_home = (params) => {
    return axios.post(baseurl+ '/tj_home',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const gettj_home1 = (params) => {
    return axios.post(baseurl+ '/tj_home1',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const gettj_home2 = (params) => {
    return axios.post(baseurl+ '/tj_home2',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getAnnouncements = (params) => {
    return axios.post(baseurl+ '/read_oa_Announcements',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getAnnouncementsAll = (params) => {
    return axios.post(baseurl+ '/get_oa_Announcements_list',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getAnnouncementsAll1 = (params) => {
    return axios.post(baseurl+ '/get_oa_Announcements_list1',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getAnnouncementsmodel = (params) => {
    return axios.post(baseurl+ '/get_oa_Announcements_model',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const editPassWord = (params) => {
    return axios.post(baseurl+ '/edit_PassWord',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const editSysUser = (params) => {
    return axios.post(baseurl+ '/edit_sys_User',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getSysUser = (params) => {
    return axios.post(baseurl+ '/get_sys_User',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const upLoadFiles = (params) => {
    return axios.post(baseurl+ '/UploadPhoto',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const addAssessmen = (params) => {
    return axios.post(baseurl+ '/add_rc_student_Assessmen',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const deletAssessmen = (params) => {
    return axios.post(baseurl+ '/del_rc_student_Assessmen',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const editAssessmen = (params) => {
    return axios.post(baseurl+ '/edit_rc_student_Assessmen',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getAssessmen = (params) => {
    return axios.post(baseurl+ '/get_rc_student_Assessmen_list',addAuthority(params),config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const regedit = (params) => {
    return axios.post(baseurl+ '/regedit_User',params,config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
const getregedit = (params) => {
    return axios.post(baseurl+ '/get_regedit_UserInfo',params,config).then((res) => {
        if( res && res.data) {
            return res.data.d && JSON.parse(res.data.d);
        }
        return false
    })
};
export {
    teacherLogin,
    getregedit,
    regedit,
    getAssessmen,
    editAssessmen,
    deletAssessmen,
    addAssessmen,
    upLoadFiles,
    editSysUser,
    getSysUser,
    editPassWord,
    baseurl,
    gettj_home1,
    gettj_home,
    gettj_home2,
    getAnnouncements,
    getAnnouncementsAll,
    getAnnouncementsAll1,
    getAnnouncementsmodel,
    getactivityInfo,
    editactivityinfo,
    addactivityList,
    deletactivityList,
    getactivityList,
    login,
    logout,
    addteacher,
    getteacher,
    deletteacher,
    editteacher,
    addClassRoom,
    getClassRoom,
    deletClassRoom,
    editClassRoom,
    getworklist,
    addworklist,
    deletworklist,
    getworkinfo,
    editworkinfo,
    addstudylist,
    getSysInfo,
    getstudylist,
    editstudylist,
    deletstudylist,
    getstudyInfo,
    getstudyRehabilitationInfo,
    addStudyRehabilitation,
    editStudyRehabilitation,
    deletStudyRehabilitation,
    getPlanList,
    addPlanList,
    editPlanList,
    deletPlanList,
    getStudyListsAll,
    getTeacherListsAll,
    lookPlanList,
    getCenterList,
    deletCenterList,
    editCenterList,
    addCenterList,
    getCenterInfo,
    
} 
